/**
 * @(#) NoticeViewMediator.as
 *
 * @date		2009. 03. 11.
 * @version		1.0
 * @author		Park, JuneHo
 * @description	공지사항 - Mediator
 * 				공지사항 Presentation Logic을 관리
 */
package com.skt.common_hr.fudraw.view
{
	import com.skt.common_hr.common.Variables;
	import com.skt.common_hr.common.components.CommonAlert;
	import com.skt.common_hr.common.puremvc.CommonFacade;
	import com.skt.common_hr.common.puremvc.OverViewMediator;
	import com.skt.common_hr.fudraw.FudrawFacade;
	import com.skt.common_hr.fudraw.events.NoticeEvent;
	import com.skt.common_hr.fudraw.model.FudrawWriteProxy;
	import com.skt.common_hr.fudraw.model.NoticeViewProxy;
	import com.skt.common_hr.fudraw.model.vo.NoticeDetailVO;
	import com.skt.common_hr.fudraw.view.components.FudrawList;
	import com.skt.common_hr.fudraw.view.components.NoticeView;
	import com.wcl.utils.DateUtil;
	
	import flash.events.Event;
	
	import mx.collections.ArrayCollection;
	import mx.rpc.http.HTTPService;
	
	import org.puremvc.as3.multicore.interfaces.INotification;


	public class NoticeOverViewMediator extends OverViewMediator
	{
		public static const NAME:String = "NoticeViewMediator";

		private var currentArticle:NoticeDetailVO;
		private var contentIdx:String;
		private var deleteFile:HTTPService;


		/**
		* 생성자
		*
		* @param
		* @return void
		*/
        public function NoticeOverViewMediator( viewComponent:Object )
        {
            super( NAME, viewComponent );
        }

		/**
		* View를 초기화 한다.
		*	- View에 기본사항을 출력하고 data를 load
		* @param
		* @return void
		*/
		override protected function initializeView():void
 		{
 			super.initializeView();
 		}

 		override protected function activatedView():void
 		{
 			super.activatedView();
			view.vsNotice.selectedIndex = 0;
			loadNoticeList();
			setEventListeners();
			
			view.sDay = DateUtil.firstDateofThisYear();
			view.eDay = DateUtil.lastDateofThisYear();
			
 		}

 		override protected function closedView():void
 		{
 			super.closedView();
 			removeEventListeners();
 		}

		private function setEventListeners():void
		{
			view.addEventListener(FudrawList.NoticeAllData, loadNoticeList);
			view.addEventListener(FudrawList.NoticeKeySelect, 	onNoticeSearch);
			view.addEventListener(FudrawList.NoticeDateSelect, 	onNoticeDateSearch);
			
			view.addEventListener(FudrawList.NoticeWrite, onNoticeWrite);
			view.addEventListener(FudrawList.NoticeReload, onNoticeReloadList);
			view.addEventListener(NoticeEvent.SELECTED_ITEM, onDetailView);

			view.noticeDetail.addEventListener(NoticeView.NoticeShCancel, onNoticeDetailCancel);
			view.noticeDetail.addEventListener(NoticeEvent.MODIFY_ITEM, onNoticeModify);
			
			
			view.addEventListener(FudrawList.NoticeWriteFudraw, onNoticeWriteFudraw);
			view.addEventListener(FudrawList.NoticeDeleteFudraw, onNoticeDeleteFudraw);
			view.addEventListener(FudrawList.NoticeSelectItemFudraw, onNoticeSelectFudraw);
		}

 		private function removeEventListeners():void
 		{
			view.removeEventListener(FudrawList.NoticeAllData, loadNoticeList);
			view.removeEventListener(FudrawList.NoticeKeySelect, onNoticeSearch);
			view.removeEventListener(FudrawList.NoticeWrite, onNoticeWrite);
			view.removeEventListener(FudrawList.NoticeReload, onNoticeReloadList);
			view.removeEventListener(NoticeEvent.SELECTED_ITEM, onDetailView);
			view.noticeDetail.removeEventListener(NoticeView.NoticeShCancel, onNoticeDetailCancel);
			view.noticeDetail.removeEventListener(NoticeEvent.MODIFY_ITEM, onNoticeModify);
			
			view.removeEventListener(FudrawList.NoticeWriteFudraw, onNoticeWriteFudraw);
			view.removeEventListener(FudrawList.NoticeDeleteFudraw, onNoticeDeleteFudraw);


			
			
 		}

 		//공지사항 쓰기/수정 버튼 권한체크
 		private function setAuthority1():void
 		{
 			view.writeBtn.visible = false;
			for each(var auth:* in Variables.authorities)
			{
				//trace(auth.EMENU + ", " +auth.EMERL );
				if(auth.EMENU == "FUDRAW") {
					if(auth.EMERL == "W") {
						view.writeBtn.visible = true;
						view.noticeDetail.editBtnValue = true;
						view.chkPastView.visible = true;
					}
				}
			}
 		}

  		private function loadNoticeList(event:Event = null):void
 		{
			var noticeProxy:NoticeViewProxy = facade.retrieveProxy(NoticeViewProxy.NAME) as NoticeViewProxy;
			noticeProxy.loadList(view.chkPastView.selected);
			noticeProxy.loadList_Fudraw();
			
			//버튼권한체크 
			setAuthority1();
 		}


 		private function onNoticeReloadList(event:Event = null):void
 		{
			var noticeProxy:NoticeViewProxy = facade.retrieveProxy(NoticeViewProxy.NAME) as NoticeViewProxy;
			noticeProxy.loadList(view.chkPastView.selected);
 		}



		///////////////////////////////////////////////////////////////////////////////////////////////////
		//
		//	Process MVC Messages
		//
		///////////////////////////////////////////////////////////////////////////////////////////////////
		/**
		* 본 Class에서 처리해야할 Noti들을 등록한다.
		*
		* @param
		* @return void
		*/
		override public function listNotificationInterests():Array
		{
			return basicInterestMessages.concat([
					FudrawFacade.LOAD_NOTICE_COMPLETE,
					FudrawFacade.LOAD_NOTICE_COMPLETE_FUDRAW,					
					FudrawFacade.MODE_LIST_NOTICE
					]);
		}


		/**
		* Noti별로 처리한다.
		*
		* @param
		* @return void
		*/
		override public function handleNotification(note:INotification):void
		{
			super.handleNotification(note);
			switch(note.getName())
			{
				case FudrawFacade.LOAD_NOTICE_COMPLETE:
					displayNoticeList(note.getBody() as NoticeViewProxy);
					break;
				
				// FADRAW 조회 완료시 
				case FudrawFacade.LOAD_NOTICE_COMPLETE_FUDRAW:
					displayNoticeList_FUDRAW(note.getBody() as NoticeViewProxy);
					break;					
					
				case FudrawFacade.MODE_LIST_NOTICE:
					view.vsNotice.selectedIndex = 0;

					loadNoticeList();

//					if(Boolean(note.getBody())) {
//						loadNoticeList();
//					}
					break;
				default:
					//trace(this.instanceID+"에서 처리 안된 Noti가 검출됨 : " + note.getBody());
					break;
			}
		}



		/**
		 * Proxy에서 DataLoad를 완료한 후 호출됨
		 *     - load된 데이터를 화면에 출력
		 * @param
		 * @return void
		 */
 		private function displayNoticeList(proxy:NoticeViewProxy):void
 		{
 			//view.pageNavigator.TotalPageCount = proxy.rowCount;	//총페이지
 			view.dataProvider = proxy.noticeList;
 			CommonFacade(facade).setStatusMessage("STATUS_COMPLETE_LOAD_NOTICE_LIST", [view.dataProvider.length]);
 		}
 		
 		public var acCode:ArrayCollection;
 		public var ac_fuDrawList:ArrayCollection;
 		private function displayNoticeList_FUDRAW(proxy:NoticeViewProxy):void
 		{
 			//view.pageNavigator.TotalPageCount = proxy.rowCount;	//총페이지
 			ac_fuDrawList 				= proxy.ac_fuDrawList;
 			acCode 						= proxy.ac_Code;
 			view.dg2.dataProvider	= ac_fuDrawList;
 			view.lbLtaget.text			= proxy.sLtarget_list;
 			CommonFacade(facade).setStatusMessage("STATUS_COMPLETE_LOAD_NOTICE_LIST", [view.dataProvider.length]);
 		} 		



		private function onNoticeSearch(event:Event):void
 		{
// 			if(view.keyWordNm == "") {
// 				CommonAlert.showMessage(commonFacade.bundle, "ALERT_NO_KEYWORD");
// 				return ;
// 			}
 			var noticeProxy:NoticeViewProxy = facade.retrieveProxy(NoticeViewProxy.NAME) as NoticeViewProxy;
			noticeProxy.keywordSearch("1", view.keyWordNm);
 		}

		private function onNoticeDateSearch(event:Event):void
 		{
 			if(view.keyWordNm == "") {
 				CommonAlert.showMessage(commonFacade.bundle, "ALERT_NO_KEYWORD");
 				return ;
 			}
 			var noticeProxy:NoticeViewProxy = facade.retrieveProxy(NoticeViewProxy.NAME) as NoticeViewProxy;
			noticeProxy.keywordSearch("1", view.keyWordNm);
 		}




















		///////////////////////////////////////////////////////////////////////////////////////////////////
		//
		//	Event handler (from View, Internal or....)
		//
		///////////////////////////////////////////////////////////////////////////////////////////////////
		private function onNoticeWrite(event:Event):void
 		{
			view.vsNotice.selectedIndex = 2;
 			facade.sendNotification(FudrawFacade.MODE_WRITE_NOTICE, this);
 		}

		private function onNoticeWriteFudraw(event:Event):void
 		{
			view.vsNotice.selectedIndex = 3;
 			facade.sendNotification(FudrawFacade.MODE_WRITE_NOTICE_FUDRAW, this);
 		}

		private function onNoticeDeleteFudraw(event:Event):void
 		{
 			var fudrawProxy:FudrawWriteProxy = facade.retrieveProxy(FudrawWriteProxy.NAME) as FudrawWriteProxy;
			fudrawProxy.deleteData(view.delKey); 			
 		} 		

		private function onNoticeSelectFudraw(event:Event):void
 		{
			view.vsNotice.selectedIndex = 3;
			
 			facade.sendNotification(FudrawFacade.MODE_MODIFY_NOTICE_FUDRAW, this);
 		}


		private function onDetailView(event:NoticeEvent):void
		{
			view.vsNotice.selectedIndex = 1;
			view.noticeDetail.dataProvider = event.selectedItem;			
		}

		private function onNoticeDetailCancel(event:Event):void
		{
			view.vsNotice.selectedIndex = 0;
			loadNoticeList();
		}

 		private function onNoticeModify(event:Event):void
 		{
			view.vsNotice.selectedIndex = 2;
			facade.sendNotification(FudrawFacade.MODE_MODIFY_NOTICE, view.noticeDetail.dataProvider);
 		}



 		private function clearWriteView():void
 		{
 			currentArticle = null;
			view.noticeWrite.dataProvider = null;
			view.noticeWrite.content.htmlText = "";
 		}



		///////////////////////////////////////////////////////////////////////////////////////////////////
		//
		//	Utility Functions
		//
		///////////////////////////////////////////////////////////////////////////////////////////////////
        public function get view():FudrawList
        {
            return viewComponent as FudrawList;
        }
	}
}